package cn.lg.soar.activiti.controller;

import cn.lg.soar.activiti.api.utils.ProcessUtil;
import cn.lg.soar.activiti.service.ActTaskService;
import cn.lg.soar.activiti.vo.HistoryTaskQO;
import cn.lg.soar.activiti.vo.HistoryTaskVO;
import cn.lg.soar.activiti.vo.TaskQO;
import cn.lg.soar.activiti.vo.TaskVO;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.FileUtil;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.system.api.service.IOrgApi;
import cn.lg.soar.system.api.service.IUserApi;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@RestController
@RequestMapping("/task")
public class TaskController {

	private final ActTaskService actTaskService;
	private final IUserApi userApi;
	private final IOrgApi orgApi;

	public TaskController(ActTaskService actTaskService, IUserApi userApi, IOrgApi orgApi) {
		this.actTaskService = actTaskService;
		this.userApi = userApi;
		this.orgApi = orgApi;
	}

	@GetMapping("todo")
	@Operation(summary = "待办", description = "待办")
	public IPage<TaskVO> todo(TaskQO qo) {
		Set<String> candidates = new HashSet<>();
		candidates.add(UserContext.getUser().getUsername());
		// 查询当前用户角色
		List<Long> roleIds = userApi.getUserCurrentAllRoleIds(UserContext.getUserId());
		ProcessUtil.roleToCandidateBatch(roleIds).forEach(candidates::add);
		// 查询当前用户部门
		List<Long> orgIds = orgApi.idsByUserId(UserContext.getUserId());
		ProcessUtil.orgToCandidateBatch(orgIds).forEach(candidates::add);
		// 查询
		return actTaskService.todo(qo, candidates);
	}

	@GetMapping("done")
	@Operation(summary = "待办", description = "待办")
	public IPage<HistoryTaskVO> done(HistoryTaskQO qo) {
		qo.setAssignee(UserContext.getUser().getUsername());
		return actTaskService.done(qo);
	}

	@GetMapping("/view/{id}")
	@Operation(summary = "查询", description = "查询")
	public ResponseEntity viewCurrentImage(@PathVariable String id) {
		InputStream imageStream = actTaskService.viewByTaskId(id);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_PNG);
		return new ResponseEntity(FileUtil.readBytes(imageStream), headers, HttpStatus.CREATED);
	}

	@GetMapping("/comment/{id}")
	@Operation(summary = "查询", description = "查询")
	public HttpResult commitList(@PathVariable String id) {
		return HttpResult.data(actTaskService.getCommentByTaskId(id));
	}

	@DeleteMapping
	@Operation(summary = "删除", description = "删除")
	public HttpResult submitTask(@RequestBody String[] ids) {
		actTaskService.delTasks(ids);
		return HttpResult.success();
	}

}
